線編輯 (Line Edits):實際位置、實際速度、指令速度
線編輯用來展示實際位置和實際速度,並獲取使用者輸入的指令速度,該編輯的名稱為 leActPosition、leActVelocity 及 leCommVelocity。
使用變數 pos
及 vel
以獲取實際位置和速度,其初始值皆為設為零,透過連接至信號 sendData 的 updateAxisData 位置獲取實際位置及速度,在 連接鈕 中的計時器章節我們說明過 updateAxisData,當發送 sendData 時將呼叫 updateAxisData,其使用 setText 更新 leActPosition 及 leActVelocity,當中使用 number 來獲取實際位置和速度,並將數字格式設為 f(無科學計數法的十進制數),並將小數點後數值設為三位。
以下代碼在 QtGui.cpp
中:
QObject::connect(ks, &ksWorker::sendData, this, &QtGui::updateAxisData);
以下代碼在 ksworker.cpp
中:
QObject::connect(dataTimer, &QTimer::timeout, [this]()
{
if (maSts.State == ecatOP && currentIndex >= 0)
{
double pos = 0.0;
double vel = 0.0;
data.powerStatus = powerStatus[currentIndex];
nRet = GetAxisPosition(currentIndex, McSource::mcActualValue, &pos);
if (nRet == KsError::errNoError)
data.actualPos = pos;
else
data.actualPos = (double)nRet;
nRet = GetAxisVelocity(currentIndex, McSource::mcActualValue, &(vel));
if (nRet == KsError::errNoError)
data.actualVel = vel;
else
data.actualVel = (double)nRet;
}
emit sendData(data);
});
以下代碼在 QtGui.cpp
中:
void QtGui::updateAxisData(axisData data)
...........
ui->leActPosition->setText(QString::number(data.actualPos, 'f', 3));
ui->leActVelocity->setText(QString::number(data.actualVel, 'f', 3));
}
於 leCommVelocity,我們宣告變數 commandVelocity 並將其設為 360 (ksworker.h and ksworker.cpp),接著將信號 textChanged 連接至位置 commandVelocityChanged (QtGui.cpp),當更改 leCommVelocity 中的文字時,程式將執行 commandVelocityChanged,commandVelocity 將設為所輸入的文字,並轉換為整數。
以下代碼在 ksworker.cpp
中:
ksWorker::ksWorker(QObject *parent)
: QObject(parent), dataTimer(new QTimer(this)), data({false, 0.0, 0.0}),
maSts({ ecatOffline, ecatOffline, 0, 0, 0, { ecatOffline },
{ ecatOffline }, { axisOffline } }), wkState(disconnected),
currentIndex(-1), commandVelocity(360)
以下代碼在 QtGui.cpp
中:
QObject::connect(ui->leCommVelocity, &QLineEdit::textChanged, ks, &ksWorker::commandVelocityChanged);
以下代碼在 ksworker.cpp
中:
void ksWorker::commandVelocityChanged(const QString &text)
{
if (text.toInt() >= 0)
commandVelocity = text.toInt();
}